using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Timers;
using SpoonCore2.Client;
using SpoonCore2.Message;
using SpoonCore2.Tools;

namespace SpoonCore2.Service.Broadcast
{
	class BroadcastTest
	{
		private readonly Timer _timer = new Timer(2000);
		private readonly BroadcastGroup _group;

		public BroadcastTest(ServiceManager manager)
		{
			BroadcastService service = manager.Get<BroadcastService>();
			_group = service.Join(SpoonIdentifier.Parse("16113AC34F86EAA04561F6FA24B46511"));
			_group.Register(new BroadcastGroup.ReceiveBroadcastMessageHandler<BroadcastTestMessage>(ReceiveTestMessage));

			_timer.Elapsed += _timer_Elapsed;
			_timer.Start();
		}

		private static void ReceiveTestMessage(BroadcastGroup group, BroadcastTestMessage message)
		{
			ColorConsole.WriteLine("Receive Test from " + group.Id, ConsoleColor.Blue);
		}

		private void _timer_Elapsed(object sender, ElapsedEventArgs e)
		{
			_group.Send(new BroadcastTestMessage());
		}

		private class BroadcastTestMessage : AbstractMessage
		{
			public override void GetBytes(BinaryWriter bw)
			{

			}

			public override void SetBytes(IService service, BinaryReader br)
			{

			}
		}
	}
}
